package base.day25.ticket;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyLock1 implements Runnable{
    private int ticket=100;
    private Object obj=new Object();
    private ReentrantLock rl=new ReentrantLock();


    @Override
    public void run() {
        while (true){
            try {
                rl.lock();    //手动上锁
                if (ticket==0){
                    break;
                }else {
                        Thread.sleep(100);
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "在买票，还剩下" + ticket + "张");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                rl.unlock();   //手动关锁
            }
        }
    }
}
